/**
 * 麻将结算窗口
 */
var Ma_JieSuanWindow = JieSuanWindow.extend({
    /**
     * 胡牌类型
     */
    HU_TYPE: {
        LIUJU: 0,
        ZIMO: 1,
        DIANPAO: 2,
        JIESAN: 3
    },
    /**
     * 给ccsname赋值
     * @protected
     */
    _ccsFileName :res.Ma_JieSuanWindow_json,
    /**
     * 初始化界面
     * @private
     */
    _initInfo: function () {
        var self = this;
        self.isZimo = (self._data['hu_type'] == self.HU_TYPE.ZIMO);
        self.isLiuju = (self._data['hu_type'] == self.HU_TYPE.LIUJU);
        self.isDianpao = (self._data['hu_type'] == self.HU_TYPE.DIANPAO);
        self.isJiesan = (self._data['hu_type'] == self.HU_TYPE.JIESAN);
        self.my_score = 0;
        self._setWanFa();
        self._setTime();
        self._setRoomId();
        self._setPlayers();
        self._setTile();
        self._setBtn();
        self._setZhuanMa();
        self._setEnd();
    },
    /**
     * 结束
     * @private
     */
    _setEnd: function () {

    },
    /**
     * 设置玩法名称
     * @private
     */
    _setWanFa: function () {
        var self = this;
        var wanfa = (self._data['map_id'] ? ('玩法：' + gameData.mapId2Name(self._data['map_id'])) : '玩法：');
        this._nodeList["lb_wanfa"].setString(wanfa);
    },
    /**
     * 设置时间
     * @private
     */
    _setTime: function () {
        var self = this;
        this._nodeList["lb_ts"].setString(timestamp2time(self._data['ts']));
    },
    /**
     * 设置房间号/局数
     */
    _setRoomId: function () {
        var self = this;
        var roomIdStr = '房号：' + gameData.roomId + '   局数：' + self._data['cur_round'] + '/' + self._data['total_round'];
        this._nodeList["lb_roomid"].setString(roomIdStr);
    },
    /**
     * 设置玩家数据
     * @private
     */
    _setPlayers: function () {
        var self = this;
        var players = self._data.players;
        for (var i = 0; i < players.length; i++) {
            var player = players[i];
            var uid = player['uid'];
            var score = player['score'];
            if (uid == gameData.uid) {
                self.my_score = score;
            }
            self._setRowUnit(i, player);
        }
        for (; i < 4; i++) {
            var RowUnit = this._nodeList["row" + i];
            if (RowUnit) {
                RowUnit.setVisible(false);
            }
        }
    },
    /**
     * 设置玩家牌数据
     * @param index
     * @param player
     * @private
     */
    _setRowUnit: function (index, player) {
        var self = this;
        var RowUnit = this._nodeList["row" + index];
        var uid = player.uid;
        /**
         * 名字
         */
        var playerInfo = gameData.getPlayerInfoByUid(uid);
        RowUnit["lb_nickname"].setString(ellipsisStr(playerInfo.nickname, 6));
        /**
         * 庄
         */
        var zhuangUid = self._data['zhuang'];
        RowUnit["sp_zhuang"].setVisible(zhuangUid == uid);
        self._setRowDescribe(index, player);
        self._setScore(index, player);
        self._setRowHuType(index, player);
        self._setRowPai(index, player);
    },
    /**
     * 设置玩家胡类型
     * @param index
     * @param playerData
     * @private
     */
    _setRowHuType: function (index, playerData) {
        var self = this;
        var RowUnit = self._nodeList["row" + index];
        var uid = playerData.uid;
        var dianpaoUid = self._data['dianpao_uid'];
        var isHu = playerData['hu'];
        if (self.isDianpao) {
            if (uid == dianpaoUid)
                RowUnit["dianpao"].setVisible(true);
            else if (isHu)
                RowUnit["hu"].setVisible(true);
        } else if (isHu && self.isZimo) {
            RowUnit["zimo"].setVisible(true);
        }
    },
    /**
     * 设置玩家牌描述
     * @param index
     * @param playerData
     * @private
     */
    _setRowDescribe: function (index, playerData) {
        var self = this;
        var RowUnit = self._nodeList["row" + index];
        var gouchengArr = [];
        var textArr = playerData['text_arr'];
        if (textArr) {
            for (var j = 0; j < textArr.length; j++) {
                var obj = textArr[j];
                for (var key in obj) {
                    gouchengArr.push(decodeURIComponent(key) + (obj[key] > 1 ? 'x' + obj[key] : ''));
                }
            }
        }
        gouchengArr.sort(function (a, b) {
            return a < b
        });
        RowUnit["lb_goucheng"].setString(gouchengArr.join(" "));
    },
    /**
     * 设置玩家分数
     * @param index
     * @param playerData
     * @private
     */
    _setScore: function (index, playerData) {
        var self = this;
        var RowUnit = self._nodeList["row" + index];
        var score = playerData['score'];
        var gangScore = playerData['gang_score'];
        var fan = playerData['fan'];
        var extra_score = playerData['extra_score'];
        //番分
        RowUnit["lb_score"].setString(fan <= 0 ? fan : ('+' + fan.toString()));
        //杠分
        RowUnit["lb_score_gang"].setString(gangScore <= 0 ? gangScore : ('+' + gangScore.toString()));
        //总分
        RowUnit["lb_score_sum"].setString(score <= 0 ? score : ('+' + score.toString()));
    },
    /**
     * 设置玩家牌
     * @param index
     * @param playerData
     * @private
     */
    _setRowPai: function (index, playerData) {
        var self = this;
        var RowUnit = this._nodeList["row" + index];
        var paiArr = playerData['pai_arr'];
        var duiArr = playerData['dui_arr'];
        var huPaiId = self._data['hu_pai_id'];
        var isHu = playerData['hu'];
        var paiIdArr = [];
        var gangIdArr = [];
        var gangIndexArr = [];
        var gangPosArr = [];
        var gapNum = 0;
        var gapDistance = 10;
        var gangIndex = 0;

        if (isHu) {
            var idx = paiArr.indexOf(huPaiId);
            if (idx >= 0) {
                paiArr.splice(idx, 1);
            }
        }
        while (true) {
            for (var j = 0; j < duiArr.length; j++) {
                var dui = duiArr[j];
                if (dui.type == 5)
                    break;
            }
            if (j == duiArr.length)
                break;
            paiArr.push(duiArr[j]['pai_arr'][0]);
            paiArr.push(duiArr[j]['pai_arr'][0]);
            paiArr.push(duiArr[j]['pai_arr'][0]);
            duiArr.splice(j, 1);
        }
        _.remove(paiArr, function (n) {
            return n == 0;
        });
        paiArr.sort(function (a, b) {
            return a - b
        });
        duiArr.sort(function (a, b) {
            if (a['pai_arr'][0] == b['pai_arr'][0]) {
                if (a['pai_arr'][1] == b['pai_arr'][1])
                    return a['pai_arr'][2] - b['pai_arr'][2];
                return a['pai_arr'][1] - b['pai_arr'][1];
            }
            return a['pai_arr'][0] - b['pai_arr'][0];
        });

        var a0 = RowUnit["a0"];
        var anGangCnt = 0;
        var gangCnt = 0;
        for (var j = 0; j < duiArr.length; j++) {
            var dui = duiArr[j];
            if (dui.type == 1 || dui.type == 2) {
                paiIdArr.push(dui['pai_arr'][0]);
                paiIdArr.push(dui['pai_arr'][1]);
                paiIdArr.push(dui['pai_arr'][2]);
            }
            if (dui.type == 3) {
                paiIdArr.push(dui['pai_arr'][0]);
                paiIdArr.push(dui['pai_arr'][0]);
                paiIdArr.push(dui['pai_arr'][0]);
                gangCnt++;
            }
            if (dui.type == 4) {
                paiIdArr.push(0);
                paiIdArr.push(0);
                paiIdArr.push(0);
                anGangCnt++;
            }
            //添加杠牌
            if (dui.type == 3 || dui.type == 4) {
                gangIdArr.push(dui['pai_arr'][0]);
                gangIndexArr.push(paiIdArr.length - 2);
            }
            paiIdArr.push(-1);
        }

        for (var j = 0; j < paiArr.length; j++) {
            paiIdArr.push(paiArr[j]);
        }

        if (isHu && huPaiId) {
            if (paiIdArr[paiIdArr.length - 1] != -1)
                paiIdArr.push(-1);
            paiIdArr.push(huPaiId);
        }

        for (var j = 0; j < paiIdArr.length; j++) {
            if (paiIdArr[j] == -1) {
                gapNum++;
                continue;
            }
            var pai = RowUnit["a" + j];
            if (!pai) {
                pai = duplicateNode(a0);
                pai.setPositionX(a0.getPositionX() + a0.getContentSize().width * 0.97 * a0.getScaleX() * (j - gapNum) + gapNum * gapDistance);
                a0.getParent().addChild(pai);
            }

            if (gangIndexArr[gangIndex] == j) {
                var x = a0.getPositionX() + a0.getContentSize().width * 0.97 * a0.getScaleX() * (j - gapNum) + gapNum * gapDistance;
                var y = a0.getPositionY() + 13 * a0.getScaleY();
                gangPosArr.push(cc.p(x, y));
                gangIndex++;
            }
            var paiName = 'p2s' + paiIdArr[j] + '.png';
            setSpriteFrameByName(pai, paiName, majiang_hn.res.pai_hn_plist);
        }
        //创建杠牌
        for (var j = 0; j < gangIdArr.length; j++) {
            var gang = RowUnit["g" + j];
            if (!gang) {
                gang = duplicateNode(a0);
                gang.setPosition(gangPosArr[j]);
                a0.getParent().addChild(gang);
                var paiName = 'p2s' + gangIdArr[j] + '.png';
                setSpriteFrameByName(gang, paiName, majiang_hn.res.pai_hn_plist);
            }
        }
    },
    /**
     * 设置标题
     * @private
     */
    _setTile: function () {
        var self = this;
        if (self.isLiuju) {
            this._nodeList["title_liuju"].setVisible(true);
        } else if (self.my_score > 0) {
            this._nodeList["title_shengli"].setVisible(true);
        } else {
            this._nodeList["title_shibai"].setVisible(true);
        }
    },
    /**
     * 设置按钮状态
     */
    _setBtn: function () {
        var self = this;
        if (self._data['is_last']) {
            this._nodeList['btn_chakan'].setVisible(true);
            this._nodeList['btn_start'].setVisible(false);
        }
        if (gameData.isReplay) {
            this._nodeList['btn_start'].setVisible(false);
            this._nodeList['btn_chakan'].setVisible(false);
            this._nodeList['btn_fenxiang'].setVisible(false);
        }
    },
    /**
     * 设置抓码
     */
    _setZhuanMa: function () {
        var self = this;
        var zhongma_arr = self._data['zhongma_arr'] || [];
        var paiArr = self._data['zhongma_pai_arr'] || [];
        if (paiArr.length) {
            for (var i = 0; i < Math.min(paiArr.length, 6); i++) {
                var pai = this._nodeList["ma.a" + i];
                var paiName = 'p2s' + paiArr[i] + '.png';
                setSpriteFrameByName(pai, paiName, majiang_hn.res.pai_hn_plist);
                if (zhongma_arr.indexOf(paiArr[i]) >= 0) {
                    pai.setColor(cc.color(255, 255, 0));
                }
            }
            for (; i < 6; i++) {
                var pai = this._nodeList["ma.a" + i]
                pai && pai.setVisible(false);
            }
            this._nodeList["ma"].setVisible(true);
            if (paiArr.length == 1) {
                this._nodeList["ma.a0"].setPositionX(120);
            }
        }
    }
});